
var mousedown = 0;
$('#root_tr').attr('name','Root').attr('folder','yes').attr('path',root);


function resize_all()
{
	if ($.browser.msie && parseInt($.browser.version) == 6)
	{
		with(document.getElementById('tree_browser'))
		{
			style.height = "100%";
			style.height = (parseInt(offsetHeight)-$('#left_column_title').height())+"px";
		}
	}
	
	with(document.getElementById('list_main_table'))
	{
		style.height = "100%";
		var body = get_page_size();
		style.height = (body.height-$('#tool_fixed_div').height() - $('#titlediv').height())+"px";
	}
	
	$('#resize_bar').css(
	{

		left:$('#left_column').width(),
		top:0,
		height:$('#left_column').height()
	});
}
resize_all();
window.onresize = function () { resize_all();}

$('#left_column').bind('contextmenu',function(event)
{
	event.preventDefault();
	event.returnValue = false;
	return false;
});


//var select_file_bg = "#C6D6EF";

//window.onselect = function (event) { if (!event) event = window.event; return false;}





//context menu


$('.mlay div').bind('mouseover',function(event)
{
	var o = $(this);
	o.addClass('mlayhover');

	if (o.attr('child_menu'))
	{
		var ch = $('#'+o.attr('child_menu'));
		ch.css(
		{
			display:'',
			zIndex:o.parent().css('z-index')+1
		});
		if (o.offset().left + o.width() + ch.width() +4 > $(window).width())
		{
			ch.css('left',o.offset().left-ch.width() + 19);
		}
		else
		{
			ch.css('left',o.offset().left+o.width()-5);
		}
		if (o.offset().top + ch.height() + 4 > $(window).height())
		{
			ch.css('top',o.offset().top-ch.height() +20);
		}
		else
		{
			ch.css('top',o.offset().top);
		}
	}
}).bind('mouseout',function(event)
{
	var o = $(this);
	o.removeClass('mlayhover');
	if (o.attr('child_menu'))
	{
		var timer = setTimeout("$('#"+o.attr('child_menu')+"').css('display','none');",200);
		eval("window."+o.attr('child_menu')+'_timer = timer;');
	}
});

$('.mlay\\ child_menu').bind('mouseover',function(event)
{
	if ($(this).hasClass('mlay'))
	{
		var timer = 'window.'+$(this).attr('id')+'_timer';
		eval("clearTimeout("+timer+")");
	}
});

var mlay = { height: $('#mlay').height(), width:$('#mlay').width() };
$('#mlay').css('display','none');
var onmlay = function (event)
{
	if (!event) event = window.event;
	setTimeout('window.on_context_delay = false;',300);
	window.on_context_delay = true;
	$('.mlay').css('display','none');
	var arr = checkfile();
	$('div.mlay_item').css('display','none');
	$('.mlay_item[for_all]').css('display','');
	if (arr['dn'] == 0)
	{
		if (arr['fn'] == 1)
		{
			$('.mlay_item[for_file]').css('display','');
			if ($('[locked=yes].file_on_select').length == 1)
			{
				$('#menu_to_unlock').css('display','');
				$('#menu_to_lock').css('display','none');
				/*
				if ($('[locked=yes].file_on_select').attr('lockedby').indexOf(window.account) != -1)
				{
					$('#menu_to_upload').css('display','');
				}
				*/
			}
		}
		else if (arr['fn'] > 1)
		{
			$('.mlay_item[for_files]').css('display','');
		}
	}
	else if (arr['fn'] == 0)
	{
		if (arr['dn'] ==1)
			$('.mlay_item[for_folder]').css('display','');
		else if(arr['dn'] > 1)
			$('.mlay_item[for_folders]').css('display','');
	}
	else if (arr['dn'] + arr['fn'] == 0)
	{
		$('.mlay_item[for_none]').css('display','');
	}
	
	var body = $('#topdiv');
	$('#mlay').css( {
		'top': (body.height() - event.pageY > mlay.height )?event.pageY:event.pageY-mlay.height,
		'left': (body.width() - event.pageX > mlay.width )?event.pageX:event.pageX-mlay.width
	}).fadeIn(30);
	window.mlay = { height: $('#mlay').height(), width:$('#mlay').width() };
	return false;
}
$(document).bind('contextmenu',onmlay);

$(document.body).bind('click',function()
{
	if (window.click_delay) return;
	//$('.file_on_select').removeClass('file_on_select');
});

//close context menu
var old_onclick = document.onclick;
var on_context_delay = false;
document.onclick = function(event)
{
	if (!event) event = window.event;
	if (window.on_context_delay) return;
	if (old_onclick) old_onclick(event);
	$('.mlay').fadeOut(300);
}

function cancel_default(event)
{
	if (!event) event = window.event; 
	try{ event.returnValue = false; } catch(e){ }
	try{ event.preventDefault(); } catch(e) { }
	return false;
}
$_('topdiv').onselectstart = cancel_default;
$_('topdiv').onselect = cancel_default;


function UrlEncode(s)
{
	return encodeURIComponent(s);
}

/*
$(window).unload(function(event)
{
	$.ajax(
	{
		url: "login.php?module=phpdisk&m=logout",
		cache: false,
		async:false,
		success: function(html)
		{
		}
	});
});
*/

//select many files
var select_div = document.createElement('div');
select_div.className = 'select_div';
select_div.style.display = 'none';
$('#topdiv').append(select_div);
var select_start = false;
var drag_start = false;



function init_drag_and_select()
{
	$('#list_tbody').bind('mousedown',function(event)
	{
		if (event.target && $(event.target).parents('[list_item]').length > 0 )
		{
			$(select_div).css(
			{
				display:''
			});
			var target = event.target.tagName.toLowerCase();
			//console.log(target);
			if ( ( $('.file_on_select').length >= 1 && target == 'span') || ( $('.file_on_select').length > 1 && (target == 'div' || target == 'span' )))
			{
				if ( $(event.target).parents('[list_item]').hasClass('file_on_select'))
				{
					document.body.onselectstart = cancel_default;
					document.body.onselect = cancel_default;
					window.started_drag = false;
					drag_start = 
					{
						x:event.pageX,
						y:event.pageY
					};
				}
			}
		}

		if (!drag_start)
		{
			select_start = 
			{
				x:event.pageX,
				y:event.pageY
			};
		}
	});
}

$(document.body)
.bind('mousemove',function(event)
{
	if (!select_start  && !drag_start) return;
	
	//drag
	if (drag_start)
	{
		//create draging content div
		window.started_drag = true;
		if ($('#drag_content').length <=0)
		{
			var div  = document.createElement('div');
			$(div).attr('id','drag_content').fadeTo(10,0.7);
			
			//append content
			$('.file_on_select div').each(function()
			{
				$(div).append(this.cloneNode(1));
				if ($.browser.msie) div.style.width = $(this).width()+'px';
			});
			
			$(document.body).append(div);
			
			$('[folder=yes]').bind('mouseover',function()
			{
				if (!$(this).hasClass('file_on_select'))
					$(this).addClass('file_on_dropable');
			}).bind('mouseout',function()
			{
				$(this).removeClass('file_on_dropable');
			});
			
		}
		
		$('#drag_content').css(
		{
			left:event.pageX+3,
			top:event.pageY+3
		});
		
		return;
	}
	else if (!select_start)
	{
		return;
	}
	
	
	
	//select
	if (event.pageX > select_start.x)
	{
		$(select_div).css(
		{
			'left':select_start.x,
			'width':event.pageX-select_start.x
		});
	}
	else
	{
		$(select_div).css(
		{
			'left':event.pageX,
			'width':-event.pageX+select_start.x
		});
	}
	if (event.pageY > select_start.y)
	{
		$(select_div).css(
		{
			'top':select_start.y,
			'height':event.pageY-select_start.y
		});
	}
	else
	{
		$(select_div).css(
		{
			'top':event.pageY,
			'height':-event.pageY+select_start.y
		});
	}
	
	var offset = $(select_div).offset();
	var top = offset.top;
	var bottom = offset.top + $(select_div).height();

	
	$('[list_item]').each(function()
	{
		var offset = $(this).offset();
		var this_top = offset.top;
		var this_bottom = this_top+$(this).height();
		if ((top<this_top && bottom > this_top) || (top<this_bottom && bottom>this_bottom) || (this_top<top && this_bottom>bottom))
		{
			$(this).addClass('file_on_select');
		}
		else
		{
			$(this).removeClass('file_on_select');
		}
	});
	
})
.bind('mouseup',function()
{
	$(select_div).css(
	{
		left:0,
		top:0,
		width:0,
		height:0,
		display:'none'
	});
	select_start = false;
	document.body.onselectstart = function(){ return true;};
	document.body.onselect = function(){ return true;}
	//drop
	if (drag_start)
	{
		try{ document.body.removeChild($_('drag_content')); } catch(e){ }
		drag_start = false;
		$('[folder=yes]').unbind('mouseover').unbind('mouseout');
		var drop_to = $('.file_on_dropable');
		
		$('.file_on_select').each(function()
		{
			var q = false;
			if ($(this).attr('path') == drop_to.attr('path') && window.started_drag)
			{
				alert(can_not_drop_here);
				q = 1;
			}
			
			if (q)
			{
				drop_to.removeClass('file_on_dropable');
				window.quit_drop = true;
			}
		});
		
		if (window.quit_drop)
		{
			window.quit_drop = false;
			return;
		}
		
		if (window.path == drop_to.attr('path'))
		{
			drop_to.removeClass('file_on_dropable');
			return;
		}
		
		if (drop_to.length > 0)
		{
			$('.file_on_select').each(function()
			{
				var q = false;

				if ($(this).attr('lockedby'))
				{
//					alert(can_not_move_locked_file);
return;
					$(this).removeClass('file_on_select');
					q = 1;
				}

				if (q)
				{
					drop_to.removeClass('file_on_dropable');
					window.quit_drop = true;
				}
			});
			if (window.quit_drop)
			{
				window.quit_drop = false;
				return;
			}
			var arr=checkfile();
			sendcomm("paste",['sdir','sfile','path','action1','from'],[arr['sdir'],arr['sfile'],drop_to.attr('path'),'move',window.path],0,0,0);
			drop_to.removeClass('file_on_dropable');
		}
	}
	
});

/*
$(document).bind('keydown', function(event)
{
	var ctrl = (event.ctrlKey || event.metaKey || event.shiftKey);
	if (event.keyCode == 65 && ctrl)
	{
		allcheck();
		event.preventDefault();
	}
	else if (event.keyCode == 67 && ctrl)
	{
		if ($('.file_on_select').length>0) filecopy("copy");
	}
	else if (event.keyCode == 86 && ctrl)
	{
		if (check_clipboard()) 
		{
			filepaste();
			event.preventDefault();
		}
	}

});
fdsa
*/


function fixPng() {
  var arVersion = navigator.appVersion.split("MSIE")
  var version = parseFloat(arVersion[1])

  if ((version >= 5.5 && version < 7.0) && (document.body.filters)) {
    for(var i=0; i<document.images.length; i++) {
      var img = document.images[i];
      var imgName = img.src.toUpperCase();
      if (imgName.indexOf(".PNG") > 0) {
        var width = img.width;
        var height = img.height;
        var sizingMethod = (img.className.toLowerCase().indexOf("scale") >= 0)? "scale" : "image"; 
        img.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src.replace('%23', '%2523').replace("'", "%27") + "', sizingMethod='" + sizingMethod + "')";
        img.src = "images/blank.gif";
        img.width = width;
        img.height = height;
        }
      }
    }
  }
fixPng();



//left and right resizable
var div = document.createElement('div');
$(div).css(
{
	zIndex:3001,
	position:'absolute',
	left:$('#left_column').width(),
	top:0,
	height:$('#left_column').height(),
	width:5,
	backgroundColor:'#fff',
	opacity:0,
	cursor:'w-resize'
})
.css('-moz-user-select','none')
.attr('id','resize_bar')
.bind('mousedown',function()
{
	document.body.onselectstart = cancel_default;
	document.body.onselect = cancel_default;
	
	$(this).attr('dragable','yes')//.css('width','20px');
	
	$(document.body).css('-moz-user-select','none');
})
.bind('mouseup',function()
{
	document.body.onselectstart = function(){return true;}
	document.body.onselect = function(){return true;}
	$('#resize_bar').removeAttr('dragable');
	$(document.body).css('-moz-user-select','');
});

document.body.appendChild(div);

$(document.body).bind('mousemove',function(event)
{
	if ($('#resize_bar').attr('dragable') == 'yes' && event.pageX > 100 && event.pageX < 800)
	{
		if ($.browser.msie)
			$('#left_column').css('width',event.pageX+3);
		else
			$('#left_column').css('width',event.pageX-2);
		$('#right_column').css('margin-left',event.pageX+3);
		$('#resize_bar').css('left',event.pageX-2);
	}
});

